package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.SendBalanceApply;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.project.SendBalanceApplyService;
import com.xpsoft.oa.service.system.AppUserService;

public class SendBalanceApplyFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(SendBalanceApplyFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proId = (String)pi.getVariable("sendBalanceApply.project.id");
		String managerUserId = (String)pi.getVariable("sendBalanceApply.managerUser.userId");
		String balanceUserId = (String)pi.getVariable("sendBalanceApply.balanceUser.userId");
		String constructionUnit = (String)pi.getVariable("sendBalanceApply.constructionUnit");
		String remark = (String)pi.getVariable("sendBalanceApply.remark");
		BigDecimal contractAmount = (BigDecimal)pi.getVariable("sendBalanceApply.contractAmount");
		BigDecimal sendAuditAmount = (BigDecimal)pi.getVariable("sendBalanceApply.sendAuditAmount");
		BigDecimal preAuditAmount = (BigDecimal)pi.getVariable("sendBalanceApply.preAuditAmount");
		BigDecimal innerAuditAmount = (BigDecimal)pi.getVariable("sendBalanceApply.innerAuditAmount");
		Date startWorkDate = (Date)pi.getVariable("sendBalanceApply.startWorkDate");
		Date endWorkDate = (Date)pi.getVariable("sendBalanceApply.endWorkDate");
		Date planAuditDate = (Date)pi.getVariable("sendBalanceApply.planAuditDate");
		Date preAuditDate = (Date)pi.getVariable("sendBalanceApply.preAuditDate");
		
		SendBalanceApply sendBalanceApply = new SendBalanceApply();
		sendBalanceApply.setConstructionUnit(constructionUnit);
		sendBalanceApply.setRemark(remark);
		sendBalanceApply.setContractAmount(contractAmount);
		sendBalanceApply.setSendAuditAmount(sendAuditAmount);
		sendBalanceApply.setPreAuditAmount(preAuditAmount);
		sendBalanceApply.setInnerAuditAmount(innerAuditAmount);
		sendBalanceApply.setStartWorkDate(startWorkDate);
		sendBalanceApply.setEndWorkDate(endWorkDate);
		sendBalanceApply.setPlanAuditDate(planAuditDate);
		sendBalanceApply.setPreAuditDate(preAuditDate);
		sendBalanceApply.setCreateDate(new Date());
		if(StringUtils.isNotBlank(managerUserId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			sendBalanceApply.setManagerUser(appUserService.get(Long.valueOf(managerUserId)));
		}
		if(StringUtils.isNotBlank(balanceUserId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			sendBalanceApply.setBalanceUser(appUserService.get(Long.valueOf(balanceUserId)));
		}
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			sendBalanceApply.setProject(projectService.get(Long.valueOf(proId)));
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		sendBalanceApply.setProcessRunId(processRun.getRunId());
		
		SendBalanceApplyService sendBalanceApplyService = (SendBalanceApplyService)AppUtil.getBean("sendBalanceApplyService");
		sendBalanceApplyService.save(sendBalanceApply);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
